<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Service\DataReturnService as DRS;
use Illuminate\Http\File;
use Illuminate\Http\Request;

// use FFMpeg;

// use FFMpeg\Coordinate\Dimension;
// use FFMpeg\Filters\Video\ResizeFilter;
// use FFMpeg\Filters\Video\RotateFilter;
// use FFMpeg\Format\Video\X264;
// use FFMpeg\Media\Video;

class UploadController extends Controller
{
    /**
     * [uploadVideo 上传视频]
     * @Author:  刘家腾
     * @DateTime 2019-05-14
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function uploadVideo(Request $request)
    {

        $arr = ['mp4', '3gp'];

        $res = $this->checkUpload($request, $arr);
        if ($res['0'] != 'ok') {
            return DRS::returnErr($res);
        }

        $path = $request->file->store('video');

        $pictrue = explode('/', $path);

        $videoName = '/video/' . date('Ymd') . '/' . $pictrue['1'];

        $request->file('file')->move('video/' . date('Ymd'), '/public/video/' . $path);

        return DRS::returnOk($videoName);
    }

    /**
     * [uploadPic 上传缩略图]
     * @Author:  刘家腾
     * @DateTime 2019-05-14
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function uploadPic(Request $request)
    {

        $arr = ['png', 'jpg', 'gif', 'jpeg'];

        $res = $this->checkUpload($request, $arr);

        if ($res['0'] != 'ok') {
            return DRS::returnErr($res);
        }

        $path = $request->file->store('video');

        $pictrue = explode('/', $path);

        $videoName = '/image/' . date('Ymd') . '/' . $pictrue['1'];

        $request->file('file')->move('image/' . date('Ymd'), '/public/image/' . $path);

        return DRS::returnOk($videoName);
    }
    /**
     * [checkUpload description]
     * @Author      yuejie89@sina.cn
     * @DateTime    2019-05-20
     * @description []
     * @param       [type]
     * @return      [type]
     */
    public function checkUpload($request, $arr = [])
    {

        if (!$request->hasFile('file')) {

            return ['err', 10001, '请选择上传文件'];
        }

        if (!$request->file('file')->isValid()) {

            return ['err', 10001, '上传失败'];
        }

        $extension = $request->file->extension();

        if (!in_array($extension, $arr)) {

            return ['err', 10001, '不支持此类扩展名'];
            die;

        }
        return ['ok'];
    }

}

// public function videopic($videoPath=''){
//     // $ffmpeg = FFMpeg::create();
//     // $video = $ffmpeg->open('D:/project/php/laravel/api/public/video/20190514/e8sqF5GcygEQ0hCw3LxsLBVOW6xSqbyql0dZ2jnI.mp4');

//     $path = [
//         'ffmpeg.binaries'  => 'D:/project/php/laravel/api/public/video/20190514/e8sqF5GcygEQ0hCw3LxsLBVOW6xSqbyql0dZ2jnI.mp4',
//         'ffmpeg.binaries' => 'D:/project/php/laravel/api/public/video/20190514/e8sqF5GcygEQ0hCw3LxsLBVOW6xSqbyql0dZ2jnI.mp4',
//         'ffprobe.binaries' => 'D:/project/php/laravel/api/public/video/20190514/e8sqF5GcygEQ0hCw3LxsLBVOW6xSqbyql0dZ2jnI.mp4',
//         'ffprobe.binaries' => 'D:/project/php/laravel/api/public/video/20190514/e8sqF5GcygEQ0hCw3LxsLBVOW6xSqbyql0dZ2jnI.mp4',
//     ];
//     $ffmpeg = FFMpeg::create($path);
//     var_dump($ffmpeg);
//     die;
//    // $video = $ffmpeg->open('video.mpeg');
// }
